package com.cn.xc.utils;

import java.util.List;

import com.cn.bean.PageInfo;
import com.cn.bean.XmlSimpleList;

/**
 * FUN: divide page
 * 
 * @author sushy
 * @date 2016-3-25
 */
public class PageUtil {

	/**
	 * 分页公共方法
	 * 
	 * @param <T>
	 * @param xmlList
	 *            要分页的结果集
	 * @param pageInfo
	 *            分页数据 new一个即可
	 * @param list
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" }) 
	public static <T> PageInfo<T> listPage(XmlSimpleList<?> xmlList, PageInfo pageInfo, List<?> pageList) {
		int beginIndex = (pageInfo.getPageIndex() - 1) * pageInfo.getPageSize();
		int endIndex = pageInfo.getPageIndex() * pageInfo.getPageSize();
		beginIndex = (beginIndex >= 0) ? beginIndex : 0;
		endIndex = (endIndex <= xmlList.size()) ? endIndex : xmlList.size();
		if (beginIndex < xmlList.size() && endIndex > 0) {
			pageList = xmlList.subList(beginIndex, endIndex);
		}
		pageInfo.setList(pageList);
		pageInfo.setTotal(xmlList.size());
		
		return pageInfo;
	}

	/**
	 * 分页公共方法
	 * 
	 * @param <T>
	 * 
	 * @param xmlList
	 *            要分页的结果集
	 * @param list
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static <T> PageInfo<T> listPage(List<?> list, PageInfo pageInfo) {
		List<?> pageList = null;
		int beginIndex = (pageInfo.getPageIndex() - 1) * pageInfo.getPageSize();
		int endIndex = pageInfo.getPageIndex() * pageInfo.getPageSize();
		beginIndex = (beginIndex >= 0) ? beginIndex : 0;
		endIndex = (endIndex <= list.size()) ? endIndex : list.size();
		if (beginIndex < list.size() && endIndex > 0) {
			pageList = list.subList(beginIndex, endIndex);
		}
		pageInfo.setList(pageList);
		pageInfo.setTotal(list.size());
		
		return pageInfo;
	}
}
